package com.maikang.exammall.util.validator;

import javax.validation.ConstraintValidator;
import javax.validation.ConstraintValidatorContext;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

public class MobileOrNullValidator implements ConstraintValidator<MobileOrNull, Object> {
    private final static String patternStr = "1[3|4|5|7|8][0-9]\\d{8}";
    private final static Pattern pattern = Pattern.compile(patternStr);

    @Override
    public void initialize(MobileOrNull mobileOrNull) {
    }

    @Override
    public boolean isValid(Object o, ConstraintValidatorContext constraintValidatorContext) {
        if (o == null) return true;
        Matcher matcher = pattern.matcher(o.toString());
        return matcher.find();
    }
}
